package edu.xhu.search.controller;

import edu.xhu.model.common.dtos.ResponseResult;
import edu.xhu.search.service.SearchService;
import edu.xhu.utils.common.SearchPrefixUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@Api(value = "搜索Controller层", tags = "搜索Controller层")
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/bilisearch")
public class SearchController {

    private final SearchService searchService;

    /**
     * 根据关键词搜索视频
     *
     * @param keyword 关键词
     * @param pageNo  第几页
     * @return 视频列表
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/searchVideo")
    @ApiOperation("根据关键词搜索视频")
    public ResponseResult getMatchingVideo(@RequestParam("keyword") String keyword,
                                           @RequestParam("pageNo") Integer pageNo) throws UnsupportedEncodingException {
        keyword = URLDecoder.decode(keyword, "UTF-8");  // 解码经过url传输的字符串
        return searchService.searchVideosByKeyword(keyword, pageNo, 30);
    }

    /**
     * 获取热搜词条
     *
     * @return 热搜列表
     */
    @GetMapping("/hotSearch")
    @ApiOperation("获取热搜词条")
    public ResponseResult getHotSearch() {
        return searchService.getHotSearch();
    }


    /**
     * 根据输入内容获取相关搜索推荐词
     *
     * @param keyword 关键词
     * @return 联想词列表
     */
    @GetMapping("/recommendations")
    @ApiOperation("联想词")
    public ResponseResult recommendSearch(@RequestParam("keyword") String keyword) {
        return ResponseResult.okResult(SearchPrefixUtil.searchByPrefix(keyword));
    }
}
